<template>
  <master :title="title">
    <listpage ref="listpage"></listpage>
    <modal :visible="visible" @success="success" @fail="fail" title="链接测试">
      <textarea placeholder="请输入测试SQL" style="width: 100%;border:none;" v-model="sql" rows="5"></textarea>
    </modal>
  </master>
</template>
<script type="text/javascript">
(function (G) {
var _this = null;
G.vue({
  "usingComponents": {///引入组件
    "master": "/components/Master/Master",///母版组件
    "listpage": "/components/List/Page/Page",///通用列表组件
    "modal":"/components/Modal/Modal"
  },
  "enablePullDownRefresh": true
},G.modelList({
  	modelName:'datasource',
    title:'数据源',
    listPage(list,query){
      list.searchKey = 'id,name,pjectId,createTimeString'
      list.actions.test = {
        name:'测试',
        action(event){
          _this.item = event.detail
          _this.visible = true
        }
      }
      // list.actions.password = {
      //   name:'复制密码',
      //   async action(event){
      //     try{
      //       await navigator.clipboard.writeText(event.detail.password)
      //       G.toask('已复制')
      //     }catch(e){
      //       G.alert('请手动复制以下密码:<br/>' + event.detail.password)
      //     }
      //   }
      // }
      list.actions.table = {
        name:'连接',
        async action(event){
          G.$go(`/pages/data/table?id=${event.detail.id}&dbn=${event.detail.dbn}`)
        }
      }
    },
    modeleditquery(edit,event,query){
      edit.models.password = {
        label:'密码',
        attr:{
          type:'password'
        },
        type:'input'
      }
      if(event.type == 'add') edit.models.password.rule = { NotNull: true }
      edit.models.id.type = 'input'
    }
  },{
    data:{
      visible:false,
      item:{},
      sql:'SHOW TABLES;'
    },
    methods:{
      success(data){
        G.post('/v1/datasource/test',{ id:this.item.id,content:this.data.sql },null,{ loading:true,timeout:10 * 60 * 1000 }).then(res => {
          if(!res.errorMsg) G.alert(`执行成功,请于控制台查看结果`)
          console.log(res)
          this.visible = false
        })
      },
      fail(){
        this.visible = false
      }
    },
    mounted:function(query){
      _this = this
    }
  }));
})(Y)
</script>
<style scoped>
  
</style>